﻿using SWBetongXaydung.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SWBetongXaydung.BLL
{
    class XeBLL
    {
        public static List<Xe> GetList(BETONGXAYDUNGEntities dbContext = null)
        {
            if (dbContext != null)
                return dbContext.XeDbSet.OrderBy(e => e.SoXe).ToList();
            using (var db = DBContextHelper.GetContext())
            {
                return db.XeDbSet.OrderBy(e => e.SoXe).ToList();
            }
        }

        internal static Xe GetXe(string SoXe, BETONGXAYDUNGEntities dbContext = null)
        {
            if (dbContext != null)
                return dbContext.XeDbSet.Where(e => e.SoXe == SoXe).SingleOrDefault();
            using (var db = DBContextHelper.GetContext())
            {
                return db.XeDbSet.Where(e => e.SoXe == SoXe).SingleOrDefault();
            }
        }

        internal static CODE_RESULT_RETURN Add(Xe xe)
        {
            using (var db = DBContextHelper.GetContext())
            {
                var _xe = db.XeDbSet.Where(e => e.SoXe == xe.SoXe).SingleOrDefault();
                if (_xe != null)
                    return CODE_RESULT_RETURN.TenTrung;
                else
                {
                    xe.DangSuDung = true;
                    db.XeDbSet.Add(xe);
                    db.SaveChanges();
                    return CODE_RESULT_RETURN.ThanhCong;
                }
            }
        }

        public static CODE_RESULT_RETURN Update(Xe xe)
        {
            using (var db = DBContextHelper.GetContext())
            {
                var _xe = db.XeDbSet.Where(e => e.SoXe == xe.SoXe).SingleOrDefault();
                if (_xe != null)
                {
                    _xe.DangSuDung = xe.DangSuDung;
                    _xe.IDLoaiXe = xe.IDLoaiXe;
                    _xe.IDLoaiNhienLieu = xe.IDLoaiNhienLieu;
                    db.SaveChanges();
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                else
                {
                    return CODE_RESULT_RETURN.ThatBai;
                }
            }
        }

        public static CODE_RESULT_RETURN Delete(string soXe)
        {
            using (var db = DBContextHelper.GetContext())
            {
                var _xe = db.XeDbSet.Where(e => e.SoXe == soXe).SingleOrDefault();
                if (_xe != null)
                {
                    if (_xe.PhanCongLaiXes.Count > 0 || _xe.QuyetToans.Count > 0)
                    {
                        _xe.DangSuDung = false;
                        db.SaveChanges();
                        return CODE_RESULT_RETURN.ThanhCong;
                    }
                    else
                    {
                        db.XeDbSet.Remove(_xe);
                        db.SaveChanges();
                        return CODE_RESULT_RETURN.ThanhCong;
                    }

                }
                else
                    return CODE_RESULT_RETURN.ThatBai;
            }
        }
    }
}
